/**
 * 200. 岛屿数量
 * https://leetcode.cn/problems/number-of-islands/description/
 */
class Solution1 {
    int m, n;
    boolean[][] vist;
    int ret;
    public int numIslands(char[][] grid) {
        m = grid.length;
        n = grid[0].length;
        vist = new boolean[m][n];

        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == '1' && vist[i][j] == false) {
                    ret++;
                    dfs(grid, i, j);
                }
            }
        }
        return ret;
    }
    //向量数组
    int[] dx = {0, 0, -1, 1};
    int[] dy = {-1, 1, 0, 0};
    public void dfs(char[][] grid, int i, int j) {
        //上下左右四个方向遍历
        for(int k = 0; k < 4; k++) {
            int x = i + dx[k], y = j + dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && vist[x][y] == false && grid[x][y] == '1') {
                vist[x][y] = true;
                dfs(grid, x, y);
                //不用恢复现场
            }
        }
    }
}